package com.gs.view.database;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ActionMap;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import org.jdesktop.application.Action;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.Resource;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;

import com.gs.control.MainControl;
import com.gs.model.MyEclipse;
import com.gs.model.MySql;
import com.gs.service.impl.MainServiceImpl;
import com.gs.util.PropertyUtil;
import com.gs.util.ResourceManager;
import com.gs.view.BaseViewInterface;
import javax.swing.BoxLayout;



public class DatabasePanel extends FrameView implements BaseViewInterface, DocumentListener{
	private JPanel panel;
	private JPanel topJP;
	private JPanel pjJP;
	private JPanel dbJP;
	private JTextField dbIp_textField;
	private JTextField port_textField;
	private JTextField userName_textField;
	private JPasswordField passwordField;
	private JButton btn_link;
	private ActionMap map;
	private ResourceMap resourceMap;
	private SingleFrameApplication app;
	private MainServiceImpl mainService=new MainServiceImpl();
	private MainControl mainControl;
	private MyEclipse myeclipse;
	//private List<String>
	@Resource
	private int value;
	
	public DatabasePanel(SingleFrameApplication app){		
		  super(app);
		  this.app=app;
		  init();
		
	}
	
	@Override
	public void init() {
		mainControl=new MainControl();
		myeclipse=mainControl.getMyEclipseConfig();
		resourceMap =app.getContext().getResourceMap(DatabasePanel.class);
		map=app.getContext().getActionMap(DatabasePanel.class,this);
		panel=new JPanel();
		createTop();
		createProjectsJP();
		GridBagLayout bagLayout=new GridBagLayout();
		panel.setLayout(bagLayout);
	       GridBagConstraints top_gbc=new GridBagConstraints();
	       top_gbc.fill= GridBagConstraints.BOTH;
	       top_gbc.gridx=0;
	       top_gbc.gridy=0;
	       top_gbc.gridheight=1;
	       top_gbc.weightx=0;
	       top_gbc.weighty=0;//垂直不拉伸
	       panel.add(topJP, top_gbc);
	       
	       //GridBagLayout bagLayout=new GridBagLayout();
			//panel.setLayout(bagLayout);
		       GridBagConstraints mye_gbc=new GridBagConstraints();
		       mye_gbc.fill= GridBagConstraints.BOTH;
		       mye_gbc.gridx=0;
		       mye_gbc.gridy=1;
		       mye_gbc.gridheight=2;
		       mye_gbc.weightx=0;
		       mye_gbc.weighty=0;//垂直不拉伸
		       //panel.add(topJP, mye_gbc);
	       panel.add(pjJP, mye_gbc);
	      // bagLayout.setConstraints(topJP, gbc);
	       //mainPanel.setSize(400, 400);
	}
	
	private void createTop() {
		topJP=new JPanel();
		topJP.setLayout(new BoxLayout(topJP, BoxLayout.X_AXIS));
		JLabel lblNewLabel = new JLabel("数据库服务器地址:");
		topJP.add(lblNewLabel);
		
		dbIp_textField = new JTextField(resourceMap.getString("db.ip", this));
		dbIp_textField.getDocument().addDocumentListener(this);
		//topJP.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		topJP.add(dbIp_textField);
		dbIp_textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("端口");
		topJP.add(lblNewLabel_1);
		port_textField = new JTextField(resourceMap.getString("db.Port", this));
		port_textField.getDocument().addDocumentListener(this);
		topJP.add(port_textField);
		port_textField.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("用户名");
		topJP.add(lblNewLabel_2);
		userName_textField = new JTextField(resourceMap.getString("db.Username", this));
		userName_textField.getDocument().addDocumentListener(this);
		topJP.add(userName_textField);
		userName_textField.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("密码");
		topJP.add(lblNewLabel_3);
		passwordField = new JPasswordField(resourceMap.getString("db.Pwd", this));
		passwordField.getDocument().addDocumentListener(this);
		topJP.add(passwordField);
		
		btn_link = new JButton();
		btn_link.setAction(map.get("conDB"));
		btn_link.setText(resourceMap.getString("btn.link", this));
		topJP.add(btn_link);
		
	}
	
	private void createProjectsJP() {
		
		pjJP=new JPanel();
		pjJP.setLayout(new BoxLayout(pjJP, BoxLayout.Y_AXIS));
		List<String> wpList=myeclipse.getWorkspaces();
		String[] wps = (String[]) wpList.toArray(new String[wpList.size()]);
		JLabel wp=new JLabel("选择工作空间：");
		JComboBox jBox=new JComboBox(wps);
		jBox.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				if(e.getStateChange()==ItemEvent.SELECTED) {
					List<String> list=myeclipse.getProjects().get(e.getItem().toString());
					for(String file:list) {
						System.out.println("pro>>>>"+file);
					}
				}
			}
		});
		JPanel wpListJP=new JPanel();
		wpListJP.setLayout(new BoxLayout(wpListJP, BoxLayout.X_AXIS));
		wpListJP.add(wp);
		wpListJP.add(jBox);
		pjJP.add(wpListJP);
	}
	@Action
	public void conDB(){	
		System.out.println("连接中。。。");
		MySql sql=new MySql(userName_textField.getText(), passwordField.getText()
				, dbIp_textField.getText(),port_textField.getText() , null);
		List<String> dbList=mainService.getDbList(sql);
	}
	@Action
	public void up(DocumentEvent event){
		System.out.println("up");
	}

	@Override
	public void changedUpdate(DocumentEvent event) {
		upProperties(event);
	}

	@Override
	public void insertUpdate(DocumentEvent event) {
		upProperties(event);
		
	}

	@Override
	public void removeUpdate(DocumentEvent event) {
		upProperties(event);
	}
	
	private void upProperties(DocumentEvent event){
		int len=event.getDocument().getLength();
		String text = null;
		String filePath =System.getProperty("user.dir");
		filePath=filePath+"\\src\\"+resourceMap.getResourcesDir()+"DatabasePanel.properties";
		System.out.println("filePath>"+filePath);
		Map<String, String> map=new HashMap<String, String>();
		try {
			text=event.getDocument().getText(0, len);
			//map.put(arg0, arg1)
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(event.getDocument()==dbIp_textField.getDocument()){
			map.put("db.ip", text);					
		}else if(event.getDocument()==port_textField.getDocument()){
			map.put("db.Port", text);	
		}else if(event.getDocument()==userName_textField.getDocument()){
			map.put("db.Username", text);	
		}else if(event.getDocument()==passwordField.getDocument()){
			map.put("db.Pwd", text);	
		}
		boolean isSu=PropertyUtil.writerProFile(filePath, false, map);
		System.out.println(isSu);
	}

	
	
	@Override
	public JPanel getJPanel() {
		// TODO Auto-generated method stub
		return panel;
	}

	
}
